<?php
namespace admin;
use models\BaseDao;
class AdminUser extends Admin{
    public function  __construct(){
        parent::__construct();
        $this->assign('menumark','adminuser');
    }
/**
 * 管理员列表页面
 */
    function index(){
        // 获取数据库操作对象
        $db = new BaseDao();
        // 获取全部管理员,按照ord排序
        $data = $db->select('admin','*',['ORDER'=>'id']);
        // 将数据全部分给模板
        $this->assign('data',$data);

        //标题    
        $this->assign('title','管理员列表');
        // 导入模板
        $this->display('adminuser/index');
    }
    function add(){
        // 如果$_POST['name']存在并且不为空,说明是添加动作
        if(!empty($_POST['name'])){
            $db = new BaseDao();

            $_POST['atime'] = $_POST['ltime'] = time();
            $_POST['pw'] = md5(md5('ew_'.$_POST['pw']));

            if($db->insert('admin',$_POST)){
                $this->success('/admin/adminuser','添加ok');
            }else{
                $this->error('/admin/adminuser/add','添加fail');
            }
        }
    
        $this->assign('title','添加管理员');
        $this->display('adminuser/add');

    }
    function mod($id){
        $db = new BaseDao();
        $this->assign($db->get('admin','*',['id'=>$id]));
        $this->assign('title',"修改管理员");
        $this->display('adminuser/mod');
    }
    function doupdate(){
        $id = $_POST['id'];
        unset($_POST['id']);
        if(!empty($_POST['pw'])){
            $_POST['pw'] = md5(md5('ew_'.$_POST['pw'])); 
        }else{
            unset($_POST['pw']);
        }
        $db = new BaseDao();
        if($db->update('admin',$_POST,['id'=>$id])){
            $this->success('/admin/adminuser','修改ok');
        }else{
            $this->error('/admin/adminuser/mod/'.$id,'修改fail');
        }
    }
    function del($id){
        $db = new BaseDao();
        if($id == 10){
            $this->error('/admin/adminuser','默认的管理员不可以删除');
            die();
        }
        if($db->delete('admin',['id'=>$id])){
            $this->success('/admin/adminuser','删除ok');
        }else{
            $this->error('/admin/adminuser','删除fail');
        }
    }

}